package com.hayrullah.cinema.Dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.hayrullah.cinema.Util.HibernateUtil;
import com.hayrullah.cinema.entity.IEntity;

public abstract class AbstractDao<E extends IEntity> implements IGenericDao<E> {

	@Override
	public boolean save(E entity) {
		boolean result = false;
		try {

			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.saveOrUpdate(entity);
			session.getTransaction().commit();
			result = true;
		} catch (HibernateException exception) {
			exception.printStackTrace();
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().rollback();

		}
		return result;
	}

	@Override
	public boolean delete(E entity) {
		boolean result = false;
		try {

			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.delete(entity);
			session.getTransaction().commit();
			result = true;

		} catch (HibernateException exception) {
			exception.printStackTrace();
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().rollback();
		}

		return result;
	}

	@Override
	public boolean update(E entity) {
		boolean result = false;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.update(entity);
			session.getTransaction().commit();
			result = true;
		} catch (HibernateException exception) {
			exception.printStackTrace();
			HibernateUtil.getSessionFactory().getCurrentSession()
					.getTransaction().rollback();
		}
		return result;
	}
}
